package com.tao.action.send;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.interceptor.annotations.Allowed;
import com.tao.action.BaseAction;
import com.tao.domain.Group;
import com.tao.domain.User;
import com.tao.service.group.GroupService;
import com.tao.service.user.UserService;
import com.tao.util.pack.send.SendHttpUtil;
import com.tao.util.pack.send.SendHttpUtil.OnInitMsgListener;
import com.tao.util.pack.send.SendHttpUtil.OnSendStatsExitMsgListener;
import com.tao.util.pack.send.SendHttpUtil.OnSendStatsMsgListener;
import com.tao.util.pack.Msg;

@SuppressWarnings("serial")
public class SendAction extends BaseAction implements RequestAware,SessionAware {

	// 注意，file并不是指前端jsp上传过来的文件本身，而是文件上传过来存放在临时文件夹下面的文件
	private List<File> fileuplad;

	// 提交过来的file的名字
	private List<String> fileupladFileName;

	// 提交过来的file的MIME类型
	private List<String> fileupladContentType;

	@SuppressWarnings("rawtypes")
	@Allowed
	Map request;
	@Allowed
	Map session;
	@Allowed
	UserService userService;
	@Allowed
	GroupService groupService;

	List<User> userlist;
	List<Group> grouplist;

	String users;
	String groups;

	String content;
	
	String result = "";

	@Override
	public String execute() throws Exception {

		grouplist = groupService.findAll(null);
		userlist = userService.findAll(null);

		return SUCCESS;

	}

	//发送文本信息
	public String sendGroupMsG() {
		
		 HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("UTF-8");
			
		grouplist = groupService.findAll(null);
		userlist = userService.findAll(null);
		String[] userStr = users.split(",");
		String[] groupStr = groups.split(",");
		
		List<User> ulist = new ArrayList<User>();
		List<Group> glist = new ArrayList<Group>();
			
		
		
		for(int i = 0;i<userStr.length;i++){
		for(User user:userlist){
			if(userStr[i].equals(user.getUserName())){
				ulist.add(user);
			}
		}
		}
		
		for(int i = 0;i<groupStr.length;i++){
			for(Group group:grouplist){
			if(groupStr[i].equals(group.getGroupName())){
				glist.add(group);
			}
		}
		}
		try {
		Msg msg = new Msg();
		msg.setContent(content);
		
		    SendHttpThread(ulist,glist,msg,null);
		
			response.getWriter().write("start");
		} catch (IOException e) {
			// TODO Auto-generated catch block
//			e.printStackTrace();
		}
		
		return SUCCESS;
	}

	// 上传文件到服务器并且获取文件路径
	public List<String> UploadFile() {
		String path = ServletActionContext.getServletContext().getRealPath(
				"/upload");
		List<String> list = new ArrayList<String>();
		// 写到指定的路径中
		File file = new File(path);

		// 如果指定的路径没有就创建
		if (!file.exists()) {

			file.mkdirs();
		}

		// 把得到的文件的集合通过循环的方式读取并放在指定的路径下
		if (fileuplad != null && !fileuplad.equals("")) {
			for (int i = 0; i < fileuplad.size(); i++) {
				try {

					// list集合通过get(i)的方式来获取索引
					FileUtils.copyFile(fileuplad.get(i), new File(file,
							fileupladFileName.get(i)));
					list.add(path + "/" + fileupladFileName.get(i));
				} catch (IOException e) {

					return null;
				}
			}
		}
		
		return list;

	}

	// 发送群文件
	public void sendGroupFileMsG() {
		
		
		
		 HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		try {
			// 是否已经上传文件
			List<String> filePathList = UploadFile();
			
			if (null == filePathList ||filePathList.equals("")) {
				System.out.println(""+filePathList);
				response.getWriter().print(filePathList);
				return;
			}
            
			grouplist = groupService.findAll(null);
			userlist = userService.findAll(null);
				
			String[] userStr = users.split(",");
			String[] groupStr = groups.split(",");
			
			List<User> ulist = new ArrayList<User>();
			List<Group> glist = new ArrayList<Group>();
			

			for(int i = 0;i<userStr.length;i++){
			for(User user:userlist){
				if(userStr[i].equals(user.getUserName())){
					ulist.add(user);
				}
			}
			}
			
			for(int i = 0;i<groupStr.length;i++){
				for(Group group:grouplist){
				if(groupStr[i].equals(group.getGroupName())){
					glist.add(group);
				}
			}
			}
			Msg msg = new Msg();
			
			System.out.println(ulist);
			System.out.println(glist);
			
			SendHttpThread(ulist,glist,msg,filePathList.get(0));
			
			response.getWriter().print(filePathList.get(0));
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
		}
		
	}
	
	   // 
		public void SendHttpThread(List<User> userlist,List<Group> grouplist,Msg msg,String imagepath) {
			
			SendHttpUtil sUtil = new SendHttpUtil(userlist,grouplist,msg,imagepath);
			sUtil.setmInitMsgListener(new OnInitMsgListener() {
				
				@Override
				public void onInitMsg(String text) {
					
					System.out.println("启动发信息线程");
				}
			});
			sUtil.setmSendStatsMsgListener(new OnSendStatsMsgListener() {
				
				@Override
				public void onSendStatsMsg(String text) {
					result = text;
					System.out.println(result);
					session.put("resultSend",result);
				}
			});
			sUtil.setmSendStatsExitMsgListener(new OnSendStatsExitMsgListener() {
				
				@Override
				public void onExitMsg(String text) {
					System.out.println("发信息线程完毕");
					result = text;
					session.put("resultSend", result);
				}
			});
			session.put("resultSend", "");
			sUtil.start();
			
		}
		
		public void getResult(){
			 HttpServletResponse response = ServletActionContext.getResponse();
				response.setCharacterEncoding("UTF-8");
				try {
						response.getWriter().print(session.get("resultSend"));
						
				} catch (IOException e) {
				}
		}
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public GroupService getGroupService() {
		return groupService;
	}

	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}

	public List<User> getUserlist() {
		return userlist;
	}

	public void setUserlist(List<User> userlist) {
		this.userlist = userlist;
	}

	public List<Group> getGrouplist() {
		return grouplist;
	}

	public void setGrouplist(List<Group> grouplist) {
		this.grouplist = grouplist;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Map getRequest() {
		return request;
	}

	public void setRequest(Map request) {
		this.request = request;
	}

	public List<File> getFileuplad() {
		return fileuplad;
	}

	public void setFileuplad(List<File> fileuplad) {
		this.fileuplad = fileuplad;
	}

	public List<String> getFileupladFileName() {
		return fileupladFileName;
	}

	public void setFileupladFileName(List<String> fileupladFileName) {
		this.fileupladFileName = fileupladFileName;
	}

	public List<String> getFileupladContentType() {
		return fileupladContentType;
	}

	public void setFileupladContentType(List<String> fileupladContentType) {
		this.fileupladContentType = fileupladContentType;
	}

	public Map getSession() {
		return session;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public String getUsers() {
		return users;
	}

	public void setUsers(String users) {
		this.users = users;
	}

	public String getGroups() {
		return groups;
	}

	public void setGroups(String groups) {
		this.groups = groups;
	}

	

}
